<?php
/**
 * Created by PhpStorm.
 * User: ADMIN
 * Date: 2021/6/3
 * Time: 22:33
 */
namespace app\adminapi\validate;

use app\common\model\GoodsCategory;

class GoodsList extends Base
{
    protected $rule = [
        'page' => 'integer|min:1',
        'limit' => 'integer|min:1|max:20',
        'goods_category_id' => 'integer|min:1|dataExists',
    ];

    protected $message = [
        'page.integer' => '参数page只能是数字',
        'page.min' => '参数page最小值为1',

        'limit.integer' => '参数limit只能是数字',
        'limit.min' => '参数limit最小值为1',
        'limit.max' => '参数limit最大值为20',

        'goods_category_id.integer' => '参数goods_category_id只能是数字',
        'goods_category_id.min' => '参数goods_category_id最小值为1',
        'goods_category_id.dataExists' => '商品分类不存在',
    ];

    /**
     * 商品分类是否存在
     * @param $value
     * @param $rule
     * @param $data
     * @return bool
     * @throws \think\exception\DbException
     */
    protected function dataExists($value, $rule, $data)
    {
        if(!GoodsCategory::get(['id'=>$value])) return false;
        return true;
    }

}